/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * This class corresponds to the "profile" element of the triggers_conf.xml file.
 * It associates a profile with a set of triggers.
 */
public class JBTriggerProfile implements Serializable
{
    /** Computed profile pre triggers  (key = opname, value = list of triggers). */
    private Map preTriggersMap = new HashMap();

    /** Computed profile post triggers (key = opname, value = list of triggers). */
    private Map postTriggersMap = new HashMap();

    /** List of strings (triggers names). */
    private List triggers = new ArrayList();

    /** Profile name. */
    private String name;

    /**
     * Adds a pre trigger to the list.
     *
     * @param jbTrigger pre trigger to add
     */
    public void addPreTrigger(JBTrigger jbTrigger)
    {
        String key = jbTrigger.getOpname().trim().toLowerCase();
        List list = (List) preTriggersMap.get(key);

        if (list == null)
        {
            list = new ArrayList();
            preTriggersMap.put(key, list);
        }

        list.add(jbTrigger);
    }

    /**
     * Adds a post trigger to the list.
     *
     * @param jbTrigger trigger to add
     */
    public void addPostTrigger(JBTrigger jbTrigger)
    {
        String key = jbTrigger.getOpname().trim().toLowerCase();
        List list = (List) postTriggersMap.get(key);

        if (list == null)
        {
            list = new ArrayList();
            postTriggersMap.put(key, list);
        }

        list.add(jbTrigger);
    }

    /**
     * Adds a trigger for this profile.
     *
     * @param aname trigger name
     */
    public void addTrigger(String aname)
    {
        triggers.add(aname);
    }

    /**
     * Displays the triggers configuration for this profile.
     *
     * @return String formatted triggers configuration
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;
        buf.append("name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));

        if (triggers != null)
        {
            buf.append("      List components of triggers = ");
            buf.append(System.getProperty("line.separator"));
            it = triggers.iterator();

            while (it.hasNext())
            {
                buf.append("      ...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (preTriggersMap != null)
        {
            buf.append("Map components of preTriggersMap = ");
            buf.append(System.getProperty("line.separator"));
            it = preTriggersMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append(" ...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(System.getProperty("line.separator"));
                buf.append("  ------ List components of triggers ");
                buf.append(System.getProperty("line.separator"));

                List list = (List) preTriggersMap.get(key);
                Iterator it2 = list.iterator();

                while (it2.hasNext())
                {
                    buf.append("      ...");
                    buf.append(it2.next());
                    buf.append(System.getProperty("line.separator"));
                }
            }
        }

        if (postTriggersMap != null)
        {
            buf.append("Map components of postTriggersMap = ");
            buf.append(System.getProperty("line.separator"));
            it = postTriggersMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append(" ...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(System.getProperty("line.separator"));
                buf.append("  ------ List components of triggers ");
                buf.append(System.getProperty("line.separator"));

                List list = (List) postTriggersMap.get(key);
                Iterator it2 = list.iterator();

                while (it2.hasNext())
                {
                    buf.append("      ...");
                    buf.append(it2.next());
                    buf.append(System.getProperty("line.separator"));
                }
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the profile name.
     * @return profile name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the profile name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**


    /**
     * Returns the triggers names.
     * @return List of strings
     */
    public List getTriggers()
    {
        return triggers;
    }

    /**
     * Gets pre triggers from operation type.
     *
     * @param opname requested operation type
     *
     * @return list of triggers
     */
    public List findPreTriggersByOpName(String opname)
    {
        String key = opname.trim().toLowerCase();

        return (List) preTriggersMap.get(key);
    }

    /**
     * Gets post triggers from operation type.
     *
     * @param opname requested operation type
     *
     * @return list of triggers
     */
    public List findPostTriggersByOpName(String opname)
    {
        String key = opname.trim().toLowerCase();

        return (List) postTriggersMap.get(key);
    }
}
